﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Security;

namespace haber.Yonetim
{
    /// <summary>
    /// Summary description for logoHandler
    /// </summary>
    public class logoHandler : IHttpHandler
    {

        private void UpdateCookie(string cookie_name, string cookie_value)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
            if (null == cookie)
            {
                cookie = new HttpCookie(cookie_name);
            }
            cookie.Value = cookie_value;
            HttpContext.Current.Request.Cookies.Set(cookie);
        }


        public void ProcessRequest(HttpContext context)
        {


            try
            {
                string session_param_name = "ASPSESSID";
                string session_cookie_name = "ASP.NET_SessionId";

                if (HttpContext.Current.Request.Form[session_param_name] != null)
                {
                    UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
                }
                else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
                {
                    UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);
                }
            }
            catch
            {
            }

            try
            {
                string auth_param_name = "AUTHID";
                string auth_cookie_name = FormsAuthentication.FormsCookieName;

                if (HttpContext.Current.Request.Form[auth_param_name] != null)
                {
                    UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
                }
                else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
                {
                    UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
                }

            }
            catch
            {
            }




            context.Response.ContentType = "text/plain";

            context.Response.Expires = -1;

            try
            {

                HttpPostedFile postedFile = context.Request.Files[0];



                string savepath = "";

                string tempPath = "";

              //  string dosyaAdi = context.Request["filename"];
                string tip = context.Request["tip"];
                string id= context.Request["id"];
                //if (String.IsNullOrEmpty(dosyaAdi))
                //{
                //    context.Response.StatusCode = 3117;
                //    context.Response.Write("Hata: " + "Upload Hatası. Lütfen programı kapatıp açıp tekrar yüklemeyi deneyin.");
                //    return;
                //}



                if (tip == "R")
                {
                    savepath = context.Server.MapPath("~/HaberResmi/"+id);
                 
                }else if (tip == "P") {
                        savepath = context.Server.MapPath("~/haberTV/");
                    }
                else if (tip == "X")
                    {
                        savepath = context.Server.MapPath("~/reklamResimleri/1/");
                    }
                else if (tip=="1"){
                    savepath = context.Server.MapPath("~/reklamResimleri/2/");
                }
                else if (tip == "3")
                {
                    savepath = context.Server.MapPath("~/reklamResimleri/t/");
                }
                else if (tip == "KR") {
                    savepath = context.Server.MapPath("~/anketResim/" + id);
                }
                else
                    if (tip == "KV")
                    {
                        savepath = context.Server.MapPath("~/anketVideo/" );
                    }
                    else if (tip == "YZ")
                    {
                        savepath = context.Server.MapPath("~/yazarFoto/");
                    }
                    else
                    {
                        savepath = context.Server.MapPath("~/haberEkleri/" + id);
                    }
                
                if (!Directory.Exists(savepath))
                    Directory.CreateDirectory(savepath);

                

//                String uzanti = Path.GetExtension(postedFile.FileName);

                if (File.Exists(savepath + @"\" + postedFile.FileName))
                {
                    throw new Exception("Aynı isimli dosya mevcut, lütfen yeni isimle kaydediniz.");
                }
              
                    postedFile.SaveAs(savepath + @"\" + postedFile.FileName);

                    context.Response.Write(tempPath + "/" + postedFile.FileName);

                    context.Response.StatusCode = 200;
                
            }

            catch (Exception ex)
            {

                context.Response.Write("Error: " + ex.Message);

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}